import { Injectable } from '@angular/core';
import { StateMachineService } from "@farris/command-services";
import { BindingList, BindingObject, FrameContext } from '@farris/devkit';
import { BefRepository, ResponseInfo } from '@farris/bef';
import { FormMessageService, FormNotifyService, CommandService, FormLoadingService, BeActionService } from '@farris/command-services';
import { EMPTY, of } from 'rxjs';
import { tap, map, switchMap } from 'rxjs/operators';
@Injectable()
export class BjckFormFrmControllerService {
  constructor(
    private frameContext: FrameContext,
    private messageService: FormMessageService,
    private beActionService: BeActionService,
    public stateMachineService: StateMachineService,
    public formNotifyService: FormNotifyService,
    private commandService: CommandService,
    public loadingService: FormLoadingService
  ) { }

  /**
   * 插入数据
   * @remarks 
   * @returns 
   */
  addDatas(): any {
    const context = this.frameContext;
    let id = context.bindingData.getValue(['id'])
    const rep = this.frameContext.repository as BefRepository<any>;
    const datas = this.frameContext.uiState['datas'];
    let tzDadas = []
    debugger
    for (let item of datas) {
      const tzid = item.data.id
      const gcbh = item.data.gcbh
      const wlbh = item.data.wlbh
      const wlmc = item.data.wlmc
      const wlgg = item.data.wlgg
      const wlxh = item.data.wlxh
      const wlcz = item.data.wlcz
      const dw = item.data.dw
      const wzlxid = item.data.wzlxid.wzlxid
      const lbmc = item.data.wzlxid.wzlxid_lbmc
      const ck = item.data.ck
      const jsgg = item.data.jsgg
      const cjs = item.data.cjs
      const zlbxsx = item.data.zlbxsx
      const iszn = item.data.iszn
      const jysyfx = item.data.jysyfx
      const glbm = item.data.glbm.glbm
      const glbmName = item.data.glbm.glbm_name
      tzDadas.push({
        bjlkid: {
          bjlkid: tzid,
          bjlkid_gcbh: gcbh,
          bjlkid_wlbh: wlbh,
          bjlkid_wlmc: wlmc,
          bjlkid_wlgg: wlgg,
          bjlkid_wlxh: wlxh,
          bjlkid_wlcz: wlcz,
          bjlkid_dw: dw,
          bjlkid_wzlxid_lbmc: lbmc,
          ck: ck,
          jysyfx: jysyfx,
        }
      })
    }
    rep.batchAppendByPath('/' + id + '/ckmxs', tzDadas).subscribe()
  }

  /**
   * 数量绑定字段值变化后事件
   * @remarks 
   * @returns 
   */
  slFieldValueChanged(): any {
    //获取主表id
    const id = this.frameContext.bindingData.getValue(["id"]);
    //获取子表当前行id
    const itemId = this['context']['eventParam'].id;
    debugger
    //根据主表id获取当前界面数据
    const soEntity: any = this.frameContext.repository.entityCollection.getEntityById(id);
    //根据子表id获取当前行(如有孙表参考常用操作第9个)
    const childData = soEntity.ckmxs.get(itemId);
    const tzid = childData.bjlkid.bjlkid;
    const sl = childData.sl;
    //后台赋值
    const repository = this.frameContext.repository as BefRepository<any>;
    const requestInfo = repository.restService.buildRequestInfo();
    const actionUri = 'checktzlksl';
    const body = {
      tzid: tzid,
      sl: sl,
      requestInfo: requestInfo
    }
    const action$ = this.beActionService.invokeAction(actionUri, 'PUT', null, null, body);
    return action$.pipe(
      tap((responseInfo: ResponseInfo) => {
       
      })
    )
  }
}
